package com.onenine.app.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.onenine.app.R;
import com.onenine.app.model.GroupMemberItem;
import com.onenine.app.request.GroupMemberRequest;
import com.onenine.app.response.GroupMemberResponse;
import com.onenine.app.utils.DataType;
import com.onenine.app.utils.GlideUtils;
import com.onenine.app.utils.UrlConfig;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.okhttp.OkCallback;
import com.onenine.base.okhttp.OkRequest;
import com.onenine.base.utils.Toasty;
import com.onenine.chat.rongcloud.RcUser;

import java.util.List;

/**
 * 创建时间：2018/7/4 0004 13:13
 * 创建人：fly
 * 功能描述：群组成员
 */
public class GroupMemberActivity extends BaseActivity {

    private RecyclerView mRecyclerView;

    public static void launchActivity(Context context, String targetId) {
        Intent intent = new Intent(context, GroupMemberActivity.class);
        intent.putExtra("targetId", targetId);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_group_member;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mToolbar.setCenterTitle(R.string.group_member);
        mRecyclerView = findViewById(R.id.rv_group_member);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        super.initData();
        queryGroupMemberList();
    }

    public void queryGroupMemberList() {
        String targetId = getIntent().getStringExtra("targetId");
        OkRequest.getInstance().withUrl(UrlConfig.GET_GROUP_MEMBER_LIST)
                .withRequest(new GroupMemberRequest(RcUser.getInstance().getUserId(), targetId))
                .doRequest(GroupMemberActivity.class.getSimpleName(), new OkCallback<GroupMemberResponse>() {
                    @Override
                    public void onResponse(GroupMemberResponse response, String srcData) {
                        if (response.isSuccess()) {
                            List<GroupMemberItem> groupMemberList = response.getGroupMemberList();
                            mToolbar.setCenterTitle(getString(R.string.group_member_total_count, groupMemberList.size()));
                            mRecyclerView.setAdapter(new GroupMemberAdapter(groupMemberList));
                        } else {
                            Toasty.showNormal("群成员加载失败");
                            finish();
                        }
                    }
                });
    }

    private class GroupMemberAdapter extends BaseQuickAdapter<GroupMemberItem, BaseViewHolder> {
        public GroupMemberAdapter(@Nullable List<GroupMemberItem> data) {
            super(R.layout.layout_group_member_mgr_item, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, GroupMemberItem groupMember) {
            ImageView ivAvatar = holder.getView(R.id.iv_avatar);
            GlideUtils.loadCircleAvatar(mContext, ivAvatar, groupMember.getPhoto());
            holder.setVisible(R.id.cb_select, false);
            if (groupMember.getChatOwen() == DataType.GROUP_OWNER) {
                holder.setText(R.id.tv_name, String.format("%s(群主)", groupMember.getNickName()));
            } else {
                holder.setText(R.id.tv_name, groupMember.getNickName());
            }
            holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UserDetailActivity.launchActivity(GroupMemberActivity.this, groupMember.getUserId());
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkRequest.getInstance().cancelTag(GroupMemberActivity.class.getSimpleName());
    }

}
